//
//  QualityCell.m
//  YaoHeAPP
//
//  Created by mac on 16/1/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "QualityCell.h"
#import "Masonry.h"
#import "Define.h"
#import "ProvinceAndCity.h"
@implementation QualityCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _selectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        //_selectBtn.selected=NO;
        [_selectBtn addTarget:self action:@selector(clickSelect:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_selectBtn];
        [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10);    make.height.offset(20);
            make.width.offset(20);   make.centerY.equalTo(self.mas_centerY);
        }];

        _caiZhiLabel=[[UILabel alloc]init];
        _caiZhiLabel.textColor=blackColor51;
        _caiZhiLabel.font=font15;
        [self addSubview:_caiZhiLabel];
        [_caiZhiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(40);   make.top.offset(18);
            make.height.offset(18);
        }];
        _priceLabel=[[UILabel alloc]init];
        _priceLabel.font=font15;
        _priceLabel.textColor=blackColor51;
        [self addSubview:_priceLabel];
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-8);    make.top.offset(18);
            make.height.offset(18);
        }];
        _gongCha=[[UILabel alloc]init];
        _gongCha.textColor=blackColor153;
        _gongCha.font=font14;
        [self addSubview:_gongCha];
        [_gongCha mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(40);     make.height.offset(18);
            make.top.equalTo(_caiZhiLabel.mas_bottom).offset(8);
        }];
        
        _volume=[[UILabel alloc]init];
        _volume.textColor=blackColor153;  _volume.font=font12;
        [self addSubview:_volume];
        [_volume mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-8);  make.height.offset(18);
            make.top.equalTo(_priceLabel.mas_bottom).offset(8);
        }];
        
        _weiZhiImg=[[UIImageView alloc]init];
        _weiZhiImg.image=[UIImage imageNamed:@"ic_qiugou_location"];
        [self addSubview:_weiZhiImg];
        [_weiZhiImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(40);  make.height.offset(18);  make.width.offset(18);
            make.top.equalTo(_gongCha.mas_bottom).offset(8);
        }];
        
        _locationLabel=[[UILabel alloc]init];
        _locationLabel.font=font14;   _locationLabel.textColor=blackColor153;
        [self addSubview:_locationLabel];
        [_locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_weiZhiImg.mas_right).offset(2);
            make.height.offset(18);
            make.top.equalTo(_gongCha.mas_bottom).offset(8);
        }];
        
        _timerImg=[[UIImageView alloc]init];
        _timerImg.image=[UIImage imageNamed:@"ic_qiugou_time"];
        [self addSubview:_timerImg];
        [_timerImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_locationLabel.mas_right).offset(5);
            make.top.equalTo(_gongCha.mas_bottom).offset(8);
            make.height.offset(18);    make.width.offset(18);
        }];

        _timeLabel=[[UILabel alloc]init];
        _timeLabel.numberOfLines=0;
        _timeLabel.textAlignment=NSTextAlignmentCenter;
        _timeLabel.font=font14;   _timeLabel.textColor=blackColor153;
        [self addSubview:_timeLabel];
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_timerImg.mas_right).offset(2);
            make.top.equalTo(_gongCha.mas_bottom).offset(8);
            make.right.offset(-5);
        }];
    }
    return self;
}

-(void)clickSelect:(UIButton*)select
{
    select.selected=!select.selected;
    if (select.selected) {
        [select setBackgroundImage:[UIImage imageNamed:@"ic_apply_quality_selected"] forState:UIControlStateNormal];
    }else{
        [_selectBtn setBackgroundImage:[UIImage imageNamed:@"ic_radio_btn_normal"] forState:UIControlStateNormal];
    }
    if ([self.delegate respondsToSelector:@selector(QualityCellDelegate:WithBtn:)]) {
        [self.delegate QualityCellDelegate:self WithBtn:select];
    }
}


//数据对象的set方法
-(void)setQuality:(QualityInspection *)quality
{
    if (quality!=_quality) {
        _quality=quality;
    }
    NSString*caiZhiStr=[NSString stringWithFormat:@"%@ %@/%@  %@",_quality.varietyName,_quality.materialName,_quality.surfaceName,_quality.specCode];
    _caiZhiLabel.text=caiZhiStr;
    NSString*unit=[[NSString alloc]init];
    if (_quality.priceUnit==10) {unit=@"吨";}
    if (_quality.priceUnit==20) {unit=@"张";}
    if (_quality.priceUnit==30) {unit=@"千克";}
    if (_quality.priceUnit==40) {unit=@"支";}
    NSString*jiaGe=[NSString stringWithFormat:@"¥%@%@",_quality.quotePrice,unit];
    _priceLabel.text=jiaGe;
    _gongCha.text=[NSString stringWithFormat:@"公差:%@", _quality.tolerance];
    NSString*volume=[NSString stringWithFormat:@"%@",_quality.volume];
    if ([volume isEqualToString:@"<null>"]) {
        _volume.text=@"";
    }else{
        _volume.text=[NSString stringWithFormat:@"%@%@",_quality.volume,unit];}
    NSString*shengNAME=[ProvinceAndCity inputShengID:_quality.supplyProvinceID];
    NSString*shiNAME=[ProvinceAndCity inputCityID:_quality.supplyCityID];
    _locationLabel.text=[NSString stringWithFormat:@"%@%@",shengNAME,shiNAME];
    _timeLabel.text=_quality.orderTime;
    if ([_quality.trueAndfalse isEqualToString:@"true"]) {
        _selectBtn.selected=YES;
        [_selectBtn setBackgroundImage:[UIImage imageNamed:@"ic_apply_quality_selected"] forState:UIControlStateNormal];
    }else{
        _selectBtn.selected=NO;
        [_selectBtn setBackgroundImage:[UIImage imageNamed:@"ic_radio_btn_normal"] forState:UIControlStateNormal];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
